﻿using IMark.DAO;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IMark.Admin.Account
{
    public partial class Info : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Utils.CheckAuthentication())
            {
                if (!IsPostBack)
                {
                    LoadData();
                }
            }
        }
        private void LoadData()
        {
            info.InnerHtml = string.Empty;
            if ((string)Session["Info"] != "")
            {
                info.InnerHtml = (string)Session["Info"];
                Session["Info"] = "";
            }            
            NGUOI_DUNG nguoiDung = (NGUOI_DUNG)HttpContext.Current.Session["NGUOI_DUNG"];
            txtAddress.Value = nguoiDung.DiaChi;
            txtEmail.Value = nguoiDung.Email;
            txtFullName.Value = nguoiDung.HoTen;
            txtUserName.Value = nguoiDung.TenDangNhap;
            txtPhone.Value = nguoiDung.DienThoai;
            txtBirthDay.Value = nguoiDung.NgaySinh.ToString("dd/MM/yyyy");

            StringBuilder str = new StringBuilder();
            str.Append("<select class=\"form-control\" id=\"gender\" name=\"gender\">");
            if (nguoiDung.GioiTinh == 0)
                str.Append("<option selected=\"selected\" value=\"0\">Nam</option>");
            else
                str.Append("<option value=\"0\">Nam</option>");
            if (nguoiDung.GioiTinh == 1)
                str.Append("<option selected=\"selected\" value=\"1\">Nữ</option>");
            else
                str.Append("<option value=\"1\">Nữ</option>");
            str.Append("</select>");
            lstGender.InnerHtml = str.ToString();
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            NGUOI_DUNG nguoiDung = (NGUOI_DUNG)HttpContext.Current.Session["NGUOI_DUNG"];
            nguoiDung.DiaChi = txtAddress.Value;
            nguoiDung.Email = txtEmail.Value;
            nguoiDung.HoTen = txtFullName.Value;
            nguoiDung.GioiTinh = int.Parse(Request.Form["gender"]);
            nguoiDung.DienThoai = txtPhone.Value;
            nguoiDung.NgaySinh = DateTime.ParseExact(txtBirthDay.Value, "dd/MM/yyyy", new CultureInfo("en-US"));
            bool status = NguoiDungModels.Update(nguoiDung);
            if (status == true)
            {
                Session["Info"] = "<div class='alert alert-success'>Cập nhật thông tin thành công.</div>";
                Response.Redirect("~/Admin/Account/Info.aspx");
            }
            else
                info.InnerHtml = "<div class='alert alert-danger'>Cập nhật thông tin thất bại.</div>";
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/Account/Home.aspx");
        }
    }
}